package deprecated.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import domain.dao.interfaces.IDAOContactGroup;
import util.DomainUtils;
import util.FormArgumentsException;

/**
 * Servlet implementation class AddToContactGroupServlet
 */
public class AddToContactGroupServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddToContactGroupServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String[] contactsToAdd = request.getParameterValues("contacts_to_add");
		String strIdContactGroup = request
				.getParameter("selected_contactgroup");
		int idContactGroup = -1; 
		
		ApplicationContext context = util.SpringUtils.getApplicationContext();
		
		try {
			checks(request, contactsToAdd);
			
			idContactGroup = Integer.parseInt(strIdContactGroup);
			IDAOContactGroup daoContactGroup = (IDAOContactGroup) context.getBean("beanDAOContactGroup");
			daoContactGroup.addContactsToGroup( idContactGroup, contactsToAdd);
//			DAOFactory.getDaoContactGroup().addContactsToGroup(null, idContactGroup, contactsToAdd);
			
			
			DomainUtils.displayInfoMessage(
					request, 
					DomainUtils.ADD_CONTACT_GROUP_ALERT_CURRENT_COLOR, 
					"Specified contacts had been added successfully.");
		}
		catch(Exception e) {
			
			if((e instanceof FormArgumentsException) == false) {
				request.setAttribute(DomainUtils.ALERT_ERROR_STRING, "An error occur on the server.");
				System.err.println(e);
			}	
			
			DomainUtils.keepParametersContent(request);
		}
		
		
		request.getRequestDispatcher("pages/addContactToGroup.jsp").forward(request, response);
	}

	private void checks(HttpServletRequest request, String[] contactsToAdd) 
			throws Exception {
		
		HttpSession session = request.getSession(false);

		
		//Checking session
		if(session == null || session.getAttribute(DomainUtils.CURRENT_USER_STRING) == null) {
			request.setAttribute(DomainUtils.ALERT_ERROR_STRING, 
					"You are not currently connected. May be the session expired.");
			throw new FormArgumentsException();
		}
		
		//nothing to check
		
	}
}
